package com.sgcc.activiti.service.impl;

import java.util.List;

import com.sgcc.activiti.domain.OAnomalyTypeDuration;
import com.sgcc.activiti.mapper.OAnomalyTypeDurationMapper;
import com.sgcc.common.core.utils.DateUtils;
import com.sgcc.common.security.service.TokenService;
import com.sgcc.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sgcc.activiti.mapper.OIndexMaintainMapper;
import com.sgcc.activiti.domain.OIndexMaintain;
import com.sgcc.activiti.service.IOIndexMaintainService;

/**
 * 指标维护Service业务层处理
 * 
 * @author sgcc
 * @date 2021-04-09
 */
@Service
public class OIndexMaintainServiceImpl implements IOIndexMaintainService 
{
    @Autowired
    private OIndexMaintainMapper oIndexMaintainMapper;

    @Autowired
    private OAnomalyTypeDurationMapper oAnomalyTypeDurationMapper;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询指标维护
     * 
     * @param id 指标维护ID
     * @return 指标维护
     */
    @Override
    public OIndexMaintain selectOIndexMaintainById(Long id)
    {
        return oIndexMaintainMapper.selectOIndexMaintainById(id);
    }

    /**
     * 查询指标维护列表
     * 
     * @param oIndexMaintain 指标维护
     * @return 指标维护
     */
    @Override
    public List<OIndexMaintain> selectOIndexMaintainList(OIndexMaintain oIndexMaintain)
    {
        List<OIndexMaintain> oIndexMaintainList = oIndexMaintainMapper.selectOIndexMaintainList(oIndexMaintain);
        for(OIndexMaintain oIndex : oIndexMaintainList){
            List<OAnomalyTypeDuration> oAnomalyTypeDurationList = oAnomalyTypeDurationMapper.selectoAnomalyBYIndexId(oIndex.getId());
            oIndex.setoAnomalyTypeDurationList(oAnomalyTypeDurationList);
        }
        return oIndexMaintainList;
    }

    /**
     * 新增指标维护
     * 
     * @param oIndexMaintain 指标维护
     * @return 结果
     */
    @Override
    public int insertOIndexMaintain(OIndexMaintain oIndexMaintain)
    {
        oIndexMaintain.setCreateTime(DateUtils.getNowDate());
        return oIndexMaintainMapper.insertOIndexMaintain(oIndexMaintain);
    }

    /**
     * 修改指标维护
     * 
     * @param oIndexMaintain 指标维护
     * @return 结果
     */
    @Override
    public int updateOIndexMaintain(OIndexMaintain oIndexMaintain)
    {
        // 查询出所有的指标
        List<OIndexMaintain> oIndexMaintains = oIndexMaintainMapper.selectOIndexMaintainList(new OIndexMaintain());
        long indexWeight = 0;
        for (OIndexMaintain oIndexMaintain1 : oIndexMaintains){
            if (!oIndexMaintain.getId().equals(oIndexMaintain1.getId())){
                indexWeight += oIndexMaintain1.getIndexWeight();
            }
        }
        long weight = indexWeight + oIndexMaintain.getIndexWeight();
        if(weight > 100) {
            return 0;
        }
        oIndexMaintain.setUpdateTime(DateUtils.getNowDate());
        LoginUser loginUser = tokenService.getLoginUser();
        oIndexMaintain.setUpdateBy(loginUser.getUsername());
        return oIndexMaintainMapper.updateOIndexMaintain(oIndexMaintain);
    }

    /**
     * 批量删除指标维护
     * 
     * @param ids 需要删除的指标维护ID
     * @return 结果
     */
    @Override
    public int deleteOIndexMaintainByIds(Long[] ids)
    {
        return oIndexMaintainMapper.deleteOIndexMaintainByIds(ids);
    }

    /**
     * 删除指标维护信息
     * 
     * @param id 指标维护ID
     * @return 结果
     */
    @Override
    public int deleteOIndexMaintainById(Long id)
    {
        return oIndexMaintainMapper.deleteOIndexMaintainById(id);
    }
}
